<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */

//declare(ticks=1);

use \GatewayWorker\Lib\Gateway;

/**
 * 主逻辑
 * 主要是处理 onConnect onMessage onClose 三个方法
 * onConnect 和 onClose 如果不需要可以不用实现并删除
 */
class Events
{
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     *
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        $data = [
            'type' => 'connect',
            'client_id' => $client_id
        ];
        // 向当前client_id发送数据 
        Gateway::sendToClient($client_id,json_encode($data));
        // 向所有人发送
//        Gateway::sendToAll("$client_id login\r\n");
    }

    /**
     * 当客户端发来消息时触发
     * @param int $client_id 连接id
     * @param mixed $message 具体消息
     */
    public static function onMessage($client_id, $message)
    {
        $data = json_decode($message, true);
        switch ($data['type']) {
            case "bindUid":
                self::bindUid($client_id, $data);
                $_SESSION['uid']=Gateway::getUidByClientId($client_id);
                return;
            case "userCheckUserLine":
                self::checkUserLine();
                return;
            case "sayText":
                self::sendReceiveDataOneToOne($data);
                return;
            case "userChatBindUid":
                self::chatBindUid($client_id,$data);
                $_SESSION['uid']=Gateway::getUidByClientId($client_id);
//                var_dump($data);
                return;
            case "outLine":

                return;
        }
//       $data = json_decode($message,true);
//       var_dump($data);
        // 向所有人发送 
//        Gateway::sendToAll("$client_id said $message\r\n");
    }

    /**
     * 当用户断开连接时触发
     * @param int $client_id 连接id
     */
    public static function onClose($client_id)
    {
        // 向所有人发送
        $data = [
            'msg' => "离线",
            'type' => "outLineMsg",
            'client_id' => $client_id,
            'user_id' => $_SESSION['uid']
        ];
        GateWay::sendToAll(json_encode($data));
    }

    /*
     * 一下方法 为提供调用
     * */

    // 绑定uid  上线通知
    public static function bindUid($client_id,$data){
        Gateway::bindUid($client_id,$data['user_id']);
        $data = [
            'data' => "上线",
            'user_id'=> $data['user_id'],
            'type' => "OnLine",
            'nick_name' => $data['nick_name'],
            'client_id' => $client_id
        ];
//        var_dump($data);
        GateWay::sendToAll(json_encode($data));
    }

    //绑定uid 不做通知
    public static function chatBindUid($client_id,$data){
        Gateway::bindUid($client_id,$data['user_id']);
        $data = [
            'data' => '进入聊天页面',
            'type' => "inCome",
            'client_id' => $client_id,
            'user_id' => $data['user_id']
        ];
        GateWay::sendToAll(json_encode($data));
    }

    //查询出来所有的在线的用户id
    public static function checkUserLine(){
        $userArr = Gateway::getAllUidList();
        $data = [
            'data' => $userArr,
            'type' => "userCheckUserLine",
        ];
        GateWay::sendToAll(json_encode($data));
    }

    //发送数据到彼此聊天的用户
    public static function sendReceiveDataOneToOne($data){
        Gateway::sendToUid($data['receive_id'],json_encode($data));
        Gateway::sendToUid($data['send_id'], json_encode($data));
    }
//    public static function checkAllUid(){
//
//    }
//    //离线通知
//    public static function outLine(){
//
//    }
}
